﻿using ProCP.Sim;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProCP
{
    public partial class Form3 : Form
    {
        public int ReturnValue1 { get; set; }
        public int ReturnValue2 { get; set; }
        public int ReturnValue3 { get; set; }
        public int ReturnValue4 { get; set; }
        public int ReturnValue5 { get; set; }
        public int ReturnValue6 { get; set; }
        public int ReturnValue7 { get; set; }
        public int ReturnValue8 { get; set; }

        Pen greenPen;
        Sim.Simulation simulation;
        Sim.Simulation simulationFromForm1;
        int LocationSelected;
        Form1 main;
        int[] checkVisiblity;
        public Form3(int type, int neighbors)
        {
            InitializeComponent();
            simulation = new Simulation(
                new Size(1, 1),
                new Point(50, 50),
                new Size(500, 500));
            simulation.AddCrossing(0, type);
            greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);
            DisableNumerics(neighbors);
            
            if (type == 2)
            {
                this.topCars.Location = new Point(285, 50);
                this.numericR11.Location = new Point(315, 345);
                this.numericR12.Location = new Point(361, 345);
                this.label2.Location = new Point(237, 240);
                this.label3.Location = new Point(264, 240);
                this.label2.Text = "";
                this.numericR12.Visible = false;
            }
        }
        public Form3(int type, int neighbors, int Location, Form1 f)
        {
            InitializeComponent();
            
            LocationSelected = Location;
            this.main = f;
            simulation = new Simulation(
                new Size(1, 1),
                new Point(50, 50),
                new Size(500, 500));
            simulation.AddCrossing(0, type);
            greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);
            checkVisiblity = new int[4];
            simulationFromForm1 = (Sim.Simulation)main.GetSimulation();
            DisableNumerics(neighbors);
            if (type == 2)
            {
                this.topCars.Location = new Point(285, 50);
                this.numericR11.Location = new Point(315, 345);
                this.numericR12.Location = new Point(361, 345);
                this.label2.Location = new Point(237, 240);
                this.label3.Location = new Point(264, 240);
            }

            //CheckTrafficInOut();

        }

        private void Form3_Paint(object sender, PaintEventArgs e)
        {
            simulation.Draw(e.Graphics);
            e.Graphics.DrawLine(greenPen, 350, 50, 250, 550);

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.ReturnValue1 = Convert.ToInt32(topCars.Value.ToString());
            this.ReturnValue2 = Convert.ToInt32(downCars.Value.ToString());
            this.ReturnValue3 = Convert.ToInt32(leftCars.Value.ToString());
            this.ReturnValue4 = Convert.ToInt32(rightCars.Value.ToString());
            this.ReturnValue5 = Convert.ToInt32(numericR11.Value.ToString());
            this.ReturnValue6 = Convert.ToInt32(numericR12.Value.ToString());
            this.ReturnValue7 = Convert.ToInt32(numericR21.Value.ToString());
            this.ReturnValue8 = Convert.ToInt32(numericR22.Value.ToString());
            if (checkVisiblity[0] < 1)
            {
                simulationFromForm1.cMap[LocationSelected].roads[0].trafficIn= (int)topCars.Value;
            }
            if (checkVisiblity[1] < 1)
            {
                simulationFromForm1.cMap[LocationSelected].roads[1].trafficIn = (int)rightCars.Value;
            }
            if (checkVisiblity[2] < 1)
            {
                simulationFromForm1.cMap[LocationSelected].roads[2].trafficIn = (int)downCars.Value;
            }
            if (checkVisiblity[3] < 1)
            {
                simulationFromForm1.cMap[LocationSelected].roads[3].trafficIn = (int)leftCars.Value;
            }
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void numericR12_ValueChanged(object sender, EventArgs e)
        {
            this.label2.Text = numericR12.Value.ToString();
        }

        private void numericR11_ValueChanged(object sender, EventArgs e)
        {
            this.label3.Text = numericR11.Value.ToString();
        }

        private void numericR22_ValueChanged(object sender, EventArgs e)
        {
            this.label4.Text = numericR22.Value.ToString();
        }

        private void numericR21_ValueChanged(object sender, EventArgs e)
        {
            this.label5.Text = numericR21.Value.ToString();
        }

        private void DisableNumerics(int x)
        {
            if (x % 10 == 1)
            {
                
                topCars.Visible = false;
                
                x /= 10;
                x *= 10;
                checkVisiblity[0] = 1;
            }
            if (x % 100 == 10)
            {
               rightCars.Visible = false;
                x /= 100;
                x *= 100;
                checkVisiblity[1] = 1;
            }
            if (x % 1000 == 100)
            {
                downCars.Visible = false;
                x /= 1000;
                x *= 1000;
                checkVisiblity[2] = 1;
            }
            if (x % 10000 == 1000)
            {
                leftCars.Visible = false;
                x /= 10000;
                x *= 10000;
                checkVisiblity[3] = 1;
            }
            if (checkVisiblity[0] < 1)
            {
             topCars.Value = simulationFromForm1.cMap[LocationSelected].roads[0].trafficIn;
            }
            if (checkVisiblity[1] < 1)
            {
                rightCars.Value = simulationFromForm1.cMap[LocationSelected].roads[1].trafficIn;
            }
            if (checkVisiblity[2] < 1)
            {
                downCars.Value = simulationFromForm1.cMap[LocationSelected].roads[2].trafficIn;
            }
            if (checkVisiblity[3] < 1)
            {
                leftCars.Value = simulationFromForm1.cMap[LocationSelected].roads[3].trafficIn;
            }
            //to get Traffic Lights Current Values
           
            int counter = 0;
            foreach (Sim.Road R in simulationFromForm1.cMap[LocationSelected].roads)
            {
                if(counter==1)
                {
                    for (int i = 0; i < R.GetNrOfInLanes(); i++)
                    {

                        if (i == 0)
                        {
                            numericR11.Value = R.lanesIn[i].GreenTime;

                        }
                        else if (i == 1)
                        {
                            numericR12.Value = R.lanesIn[i].GreenTime;

                        }

                           
                    }// nedted for
                }
                if (counter == 2)
                {
                    for (int i = 0; i < R.GetNrOfInLanes(); i++)
                    {
                        if (i == 0)
                        {
                            numericR21.Value = R.lanesIn[i].GreenTime;

                        }
                        else if (i == 1)
                        {
                            numericR22.Value = R.lanesIn[i].GreenTime;

                        }
                    }
                        
                
                }
                    
                    counter++;
                    
                }// end for each main
            



        }
        
    }
}
